package www.rlicker.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.model.SysUser;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.ISysUserService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.EncryptUtil;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.MessageConstant;
import www.rlicker.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;

@Controller  
@RequestMapping("/sysuser")  
public class SysUserController extends BaseController{
	
	@Autowired
	private ISysUserService service;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
        map.put("id", request.getParameter("id"));
		return "sysuser/sysUserList";
	}
	
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
		 Criteria<SysUser> param = new Criteria<SysUser>();
		 if (!StringUtil.isNullOrBlank(request.getParameter("userName"))) {
			 param.addParam("userName", request.getParameter("userName"));
		 }
         if (!StringUtil.isNullOrBlank(request.getParameter("userId"))) {
          	 param.addParam("userId", request.getParameter("userId"));
         }
         if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
        	 param.addParam("status", request.getParameter("status"));
         }
 	     if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	     }
	     if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	     }
	     List<Map<String,Object>> list = service.queryPage(param);
	     PageResult pageResult = new PageResult();
	     pageResult.setTotal(service.queryPageCount(param));
		 pageResult.setRows(list);
		 return pageResult;	
	}
	
	@RequestMapping("/detail")
	public String detail(String id,String resourceId,ModelMap map){
		if (!StringUtil.isNullOrBlank(id)) {
			SysUser sysUser = service.queryEntity(id);
			map.put("sysUser", sysUser);
		}
		List<SysRole> list = sysRoleService.queryPageList(null);
		map.put("list", list);
		map.put("resourceId", resourceId);
		return "sysuser/sysUserDetail";
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public JsonResult delete(String id,String userName){
		String ret = service.deleteById(id);
		String content ="删除了"+userName+"帐号";
		insertLog(content);
		JsonResult jr = JsonResult.newInstance(); 
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	@ResponseBody
	@RequestMapping(value="/insert" , method = { RequestMethod.POST })
	public JsonResult insert(SysUser user){
	  JsonResult jr = JsonResult.newInstance(); 
	  SysUser sysUser = service.queryEntity(user.getUserId());
	  if (sysUser != null) {
		  return jr.declareFailure(MessageConstant.USER_TYPE_0);
	  }
	  user.setPassword(EncryptUtil.encodeToken(user.getPassword()));
	  user.setCreateTime(new Date());
	  String ret = service.insert(user);
	  String content ="添加了"+user.getUserName()+"帐号";
	  insertLog(content);
	  return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	@ResponseBody
	@RequestMapping(value="/update" , method = { RequestMethod.POST })
	public JsonResult update(SysUser user){
		SysUser sysUser = service.queryEntity(user.getUserId());
		JsonResult jr = JsonResult.newInstance(); 
		Criteria<SysUser> param = new Criteria<SysUser>();
		if (!sysUser.getPassword().equals(user.getPassword())) {
			user.setPassword(EncryptUtil.encodeToken(user.getPassword()));
		}
		param.setRecord(user);
		param.addParam("user_id", user.getUserId());
	    String ret = service.update(param);
		String content ="修改了"+sysUser.getUserName()+"帐号";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	@ResponseBody
	@RequestMapping(value="/updatestatus" , method = { RequestMethod.POST })
	public JsonResult updatestatus(SysUser user){
		JsonResult jr = JsonResult.newInstance(); 
		Criteria<SysUser> param = new Criteria<SysUser>();
		param.setRecord(user);
		param.addParam("user_id", user.getUserId());
	    String ret = service.update(param);
	    String content = null;
	    if (user.getStatus() == RETURN_JSONFIVE) {
	    	content ="启用了"+user.getUserName()+"帐号";
		}else {
			content ="停用了"+user.getUserName()+"帐号";
		}
	    insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
}
